package cn.xzq.crm.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.xzq.crm.entity.Customer;
import cn.xzq.crm.entity.PageBean;
import cn.xzq.crm.execption.CustomerException;
import cn.xzq.crm.service.CustomerService;

@Controller("customerAction")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	
	@Autowired
	private CustomerService customerService;
	private Customer customer = new Customer();
	@Override
	public Customer getModel() {
		return customer;
	}
	public Customer getCustomer() {
		return customer;
	}

	//定义值栈
	private List<Customer> customers;
	public List<Customer> getCustomers() {
		return customers;
	}
	//属性封装--得到当前页,页数
	private Integer currentPage = 1;
	private Integer pageSize = 10;
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getPageSize() {
		return pageSize;
	}

	private PageBean<Customer> pageBean;
	public PageBean<Customer> getPageBean() {
		return pageBean;
	}
	//=============================================
	
	/**
	 * 转customer添加页面
	 */
	public String toAddPage(){
		return "toAddPage";
	}
	
	/**
	 * 转customer综合查询页面
	 */
	public String toQueryPage(){
		return "toQueryPage";
	}
	
	/**
	 * 转customer编辑页面
	 */
	public String toEditPage(){
		//使用模型模型驱动
		Customer f_customer = customerService.findOneById(customer.getCustId());
		if(f_customer == null){
			return "error";
		}
		//将查询到的对象保存到值栈中
		customer = f_customer;
		return "toEditPage";
	}
	
	/**
	 * 转customer列表页面
	 */
	public String toListPage(){
		List<Customer> list = customerService.findAll();
		//ServletActionContext.getRequest().setAttribute("customers", list);
		//使用值栈
		customers = list;
		return "toListPage";
	}
	
	/**
	 * 转customer分页列表页面
	 */
	@SuppressWarnings("all")
	public String toListPageBean(){
		//调用service方法封装pagebean,并存到值栈 的pageBean中
		pageBean = customerService.listPage(currentPage, pageSize);
		//System.out.println(pageBean);
		return "toListPageBean";
	}
	
	/**
	 * 添加客户
	 * @return
	 */
	public String add(){
		customerService.add(customer);
		return "add";
	}
	
	/**
	 * 客户列表
	 * @return
	 */
	public String list(){
		return null;
	}
	
	/**
	 * 删除客户
	 * @return
	 */
	public String delete(){
		//String custId = ServletActionContext.getRequest().getParameter("custId");
		//模型驱动获取custId
		try {
			customerService.delete(customer.getCustId());
		} catch (CustomerException e) {
			return "error";
		}
		return "delete";
	}
	
	/**
	 * 删除客户
	 * @return
	 */
	public String update(){
		customerService.update(customer);
		return "update";
	}
	
	/**
	 * 条件查询
	 * @return
	 */
	public String listCondition(){
		//查询条件为空（客户名为空），查全部
		//不为空则条件查询
		if(customer.getCustName().trim().isEmpty()){
			customers = customerService.findAll();
			return "toListPageBean";
		}else {
			List<Customer> list = customerService.findCondition(customer);
			customers = list;
			return "toListCondition";
		}
	}
	
	/**
	 * 多条件综合查询
	 * @return
	 */
	public String moreCondition(){
		customers = customerService.findMoreCondition(customer);
		return "moreCondition";
	}
	
	
	
}
